﻿
using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;


namespace GPS.Controllers.GPS_BaoCao
{
    public class GPS_GS_SpeedController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(ViewReportModelGPS_GS_SPEED SearchModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, SearchModel.pDVGTVT_MA);
            SearchModel.pDVGTVT_MA = getdvgtvt_Ma;
        }

        #region Index
        public ActionResult Index(ViewReportModelGPS_GS_SPEED SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            List<RepostModelGPS_GS_SPEED> model = Getdata(SearchModel);
            if (SearchModel.ExportButton == "Xuất Excel")
            {
                try
                {
                    FileInfo template = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\GPS_BaoCaoTocDo.xlsx"));
                    FileInfo newFile = new FileInfo(Server.MapPath(@"\Templates\GPS_Excel\GPS_BaoCaoTocDoNEW.xlsx"));
                    using (OfficeOpenXml.ExcelPackage excelPackage = new OfficeOpenXml.ExcelPackage(newFile, template))
                    {
                        ExcelWorkbook myWorkbook = excelPackage.Workbook;
                        ExcelWorksheet myWorksheet = myWorkbook.Worksheets["StopReport"];
                        int recordIndex = 6;
                        foreach (var item in model)
                        {
                            myWorksheet.Cells[recordIndex, 1].Value = item.STT.ToString();
                            myWorksheet.Cells[recordIndex, 2].Value = item.GPS_SPEED_BIENSO == null ? "" : item.GPS_SPEED_BIENSO.ToString();
                            myWorksheet.Cells[recordIndex, 3].Value = item.GPS_SPEED_DVGTVT_TEN == null ? "" : item.GPS_SPEED_DVGTVT_TEN.ToString();
                            myWorksheet.Cells[recordIndex, 4].Value = item.GPS_SPEED_UPDATETIME == null ? "" : item.GPS_SPEED_UPDATETIME;
                            myWorksheet.Cells[recordIndex, 5].Value = item.GPS_SPEED_SPEED == null ? "" : item.GPS_SPEED_SPEED;
                            myWorksheet.Cells[recordIndex, 6].Value = "";
                            recordIndex++;
                        }
                        myWorksheet.Cells[2, 1].Value = "Từ ngày " + SearchModel.pTUNGAY == null ? "" : SearchModel.pTUNGAY.ToString() + " đến ngày " + SearchModel.pDENNGAY == null ? "" : SearchModel.pDENNGAY.ToString();
                        myWorksheet.Cells[3, 1].Value = "Đơn vị kinh doanh vận tải: " + SearchModel.pDVGTVT_MA == null ? "" : _db.GetOne<SDONVIGTVT>(x => x.DVGTVT_MA == SearchModel.pDVGTVT_MA).DVGTVT_TEN;
                        if (SearchModel.pBIEN_SO != null)
                        {
                            myWorksheet.Cells[4, 1].Value = string.Format("Biển số: {0}", SearchModel.pBIEN_SO);
                        }
                        excelPackage.Save();
                    }
                    string contentType = string.Empty;
                    string CurrentFileName = Server.MapPath(@"\Templates\GPS_Excel\GPS_BaoCaoTocDoNEW.xlsx");
                    if (CurrentFileName.Contains(".xlsx"))
                    {
                        contentType = "application/msexcel";
                    }
                    return File(CurrentFileName, contentType, string.Format("BaoCaoTocDoVaHanhTrinhPhuongTien_{0}.xlsx", DateTime.Now.ToShortDateString().Replace("/", ":")));
                }
                catch (Exception ex)
                {
                    TempData["Message"] = ex.ToString();
                    return RedirectToAction("Index");
                }
            }
            else
            {
                ViewBag.TotalRow = model.Count();
                var pageIndex = SearchModel.Page ?? 1;
                SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
                return View(SearchModel);
            }
        }
        #endregion

        public List<RepostModelGPS_GS_SPEED> Getdata(ViewReportModelGPS_GS_SPEED SearchModel)
        {
            var pDVGTVT_MA = new OracleParameter("pDVGTVT_MA", OracleDbType.Varchar2, (SearchModel.pDVGTVT_MA == null ? DBNull.Value : (object)SearchModel.pDVGTVT_MA), ParameterDirection.Input);
            var pBIEN_SO = new OracleParameter("pBIEN_SO", OracleDbType.Varchar2, (SearchModel.pBIEN_SO == null ? DBNull.Value : (object)SearchModel.pBIEN_SO), ParameterDirection.Input);
            var pTUNGAY = new OracleParameter("pTUNGAY", OracleDbType.Date, (SearchModel.pTUNGAY == null ? DBNull.Value : (object)SearchModel.pTUNGAY), ParameterDirection.Input);
            var pDENNGAY = new OracleParameter("pDENNGAY", OracleDbType.Date, (SearchModel.pDENNGAY == null ? DBNull.Value : (object)SearchModel.pDENNGAY), ParameterDirection.Input);
            var pRET = new OracleParameter("pRET", OracleDbType.RefCursor, ParameterDirection.Output);
            var entities = _db.DbContext.Database.SqlQuery<RepostModelGPS_GS_SPEED>("BEGIN GPS_GIAMSAT.GPS_GIAMSAT_SPEED(:pDVGTVT_MA,:pBIEN_SO,:pTUNGAY,:pDENNGAY,:pRET); end;"
                                                                                                                                    , pDVGTVT_MA
                                                                                                                                    , pBIEN_SO
                                                                                                                                    , pTUNGAY
                                                                                                                                    , pDENNGAY
                                                                                                                                    , pRET).ToList();
            return entities;
        }
    }
}